#include "IPC_Client_DualCoreFlash.h"
#include "IPC_Client_System.h"
#include "g_IpcFormat.h"


extern G_IpcInitTypeDef  IpcParaInit;

uint8_t G_Client_pause_rv(void)
{
    if(MMCU->RV_CTRL.bit.CLK_EN==0x0a&&MMCU->RV_CTRL.bit.RESET==0x0a&&MMCU->RV_CTRL.bit.SLP_STATUS==0)
    {
        IpcParaInit.rv_pause_ready=0;
        IpcParaInit.rv_pause_end=0;
        if(IPC_SUCCESS!=G_Cmd_Sync_Send(IPC_TYPE_SYSTEM,IPC_SYSTEM_PAUSE_RV_CMD,NULL,0,NULL,NULL))
        {
            return ERROR;
        }
        while(IpcParaInit.rv_pause_ready!=1);
    }
    return SUCCESS;
}

uint8_t G_DualCore_flash_sectorerase(uint32_t flash_addr)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_qspi_flash_sectorerase(flash_addr);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_flash_blockerase32k(uint32_t flash_addr)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_qspi_flash_blockerase32k(flash_addr);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_flash_blockerase64k(uint32_t flash_addr)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_qspi_flash_blockerase64k(flash_addr);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_flash_write(uint32_t flash_addr, uint8_t *buf, uint32_t len)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_qspi_flash_write(flash_addr,buf,len);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_flash_read(uint32_t flash_addr, uint8_t *buf, uint32_t len)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_qspi_flash_read(flash_addr,buf,len);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

G_Boolean G_DualCore_flash_blank_check(uint32_t startaddr, uint32_t len)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    G_Boolean result;
    if(G_Client_pause_rv()==SUCCESS)
    {
        result=G_flash_blank_check(startaddr,len);
    }
    else
    {
        result=FALSE;
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_enc_write_flash(uint32_t flash_addr, uint8_t *buf, uint32_t len)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_enc_write_flash(flash_addr,buf,len);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_enc_read_flash(uint32_t flash_addr, uint8_t *buf, uint32_t len)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_enc_read_flash(flash_addr,buf,len);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_enc_read_flash_fast(uint32_t flash_addr, uint8_t *buf, uint32_t len)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_enc_read_flash_fast(flash_addr,buf,len);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_enc_erase_flash_32byte(uint32_t flash_addr)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_enc_erase_flash_32byte(flash_addr);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_enc_erase_flash_32k(uint32_t flash_addr)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_enc_erase_flash_32k(flash_addr);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_enc_earse_flash_app_area(uint32_t addr,uint32_t len)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_enc_earse_flash_app_area(addr,len);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_enc_write_download_flag(uint32_t flag)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_enc_write_download_flag(flag);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

uint8_t G_DualCore_enc_write_flash_bulk(G_ENC_BulkTypeDef *EncBulkStruct,uint32_t flash_addr, uint8_t *buf, uint32_t len)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    uint8_t result= G_Client_pause_rv();
    if(result==SUCCESS)
    {
        result=G_enc_write_flash_bulk(EncBulkStruct,flash_addr,buf,len);
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}

G_qspi_flash_size_enum G_DualCore_read_flash_size(void)
{
    uint32_t primask=__get_PRIMASK();
    __disable_irq();
    G_qspi_flash_size_enum result;
    if(G_Client_pause_rv()!=SUCCESS)
    {
        result=QFLASH_SIZE_UNKNOW;
    }
    else
    {
        result=G_read_flash_size();
    }

    IpcParaInit.rv_pause_end=1;
    if(primask==0)  __enable_irq();
    return result;
}
